package com.block.datapush.special.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.datapush.model.dto.StarInfoDTO;
import com.block.datapush.special.service.ISpecialDataStarAddupInfoService;
import com.block.datapush.special.service.ISpecialDataStarCountInfoService;
import com.block.db.entity.SpecialDataStarCountInfo;
import com.block.db.mapper.SpecialDataStarCountInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 机构数据星级统计 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-11-11
 */
@Service
public class SpecialDataStarCountInfoServiceImpl extends ServiceImpl<SpecialDataStarCountInfoMapper, SpecialDataStarCountInfo> implements ISpecialDataStarCountInfoService {

    @Autowired
    ISpecialDataStarAddupInfoService specialDataStarAddupInfoService;

//    @Async
    @Override
    @Transactional
    public void saveStarCountInfo(StarInfoDTO starInfoDTO){

        // 先保存星级回传的详情信息
        specialDataStarAddupInfoService.saveStarAddup(starInfoDTO);

        // 查询机构的统计信息
        SpecialDataStarCountInfo starCountInfo = this.baseMapper.selectEntityByInfoId(starInfoDTO.getInfoId());
        if (ObjectUtil.isEmpty(starCountInfo)){
            // 新增机构星级回传信息
            starCountInfo = this.insertStarCountInfo(starInfoDTO);
            if (ObjectUtil.isEmpty(starCountInfo)){
                return;
            }
        }

        // 更新星级回传信息
        SpecialDataStarCountInfo updateEntity = this.getUpdateEntity(starCountInfo, starInfoDTO);
        updateEntity.setId(starCountInfo.getId());
        this.updateById(updateEntity);
    }

    public SpecialDataStarCountInfo insertStarCountInfo(StarInfoDTO starInfoDTO){

        SpecialDataStarCountInfo starCountInfo = new SpecialDataStarCountInfo();
        starCountInfo.setInfoId(starInfoDTO.getInfoId());
        starCountInfo.setInfoName(starInfoDTO.getInfoName());

        boolean save = this.save(starCountInfo);
        if (save){
            return this.getById(starCountInfo);
        }
        return null;
    }

    public SpecialDataStarCountInfo getUpdateEntity(SpecialDataStarCountInfo starCountInfo, StarInfoDTO starInfoDTO){
        SpecialDataStarCountInfo updateEntity = new SpecialDataStarCountInfo();
        updateEntity.setStarNumber(starCountInfo.getStarNumber() + 1);

        if (ObjectUtil.equals(Integer.valueOf(0), starInfoDTO.getStarLevel())){
            updateEntity.setStar0Number(starCountInfo.getStar0Number() + 1);
        }

        if (ObjectUtil.equals(Integer.valueOf(1), starInfoDTO.getStarLevel())){
            updateEntity.setStar1Number(starCountInfo.getStar1Number() + 1);
        }

        if (ObjectUtil.equals(Integer.valueOf(2), starInfoDTO.getStarLevel())){
            updateEntity.setStar2Number(starCountInfo.getStar2Number() + 1);
        }

        if (ObjectUtil.equals(Integer.valueOf(3), starInfoDTO.getStarLevel())){
            updateEntity.setStar3Number(starCountInfo.getStar3Number() + 1);
        }

        if (ObjectUtil.equals(Integer.valueOf(4), starInfoDTO.getStarLevel())){
            updateEntity.setStar4Number(starCountInfo.getStar4Number() + 1);
        }

        if (ObjectUtil.equals(Integer.valueOf(5), starInfoDTO.getStarLevel())){
            updateEntity.setStar5Number(starCountInfo.getStar5Number() + 1);
        }

        return updateEntity;
    }


}
